package com.hzmg.akkacloud.common.model;

import com.hzmg.akkacloud.common.AkkaCloudSerializable;
import com.hzmg.akkacloud.common.request.WorkerHeartbeat;
import lombok.Data;

import java.util.List;

/**
 * worker info
 *
 * @author zbw
 * @since 2022/7/13
 */
@Data
public class WorkerInfo implements AkkaCloudSerializable {

    private static final long WORKER_TIMEOUT_MS = 35000;
    /**
     * akka address
     */
    private String akkaAddress;
    /**
     * web address
     */
    private String webAddress;
    private long lastActiveTime;
    private String protocol;
    private String client;
    private String tag;
    private SystemMetrics systemMetrics;

    public void refresh(WorkerHeartbeat workerHeartbeat) {
        akkaAddress = workerHeartbeat.getWorkerAddress();
        webAddress=workerHeartbeat.getWebAddress();
        lastActiveTime = workerHeartbeat.getHeartbeatTime();
        protocol = workerHeartbeat.getProtocol();
        client = workerHeartbeat.getClient();
        tag = workerHeartbeat.getTag();
        systemMetrics = workerHeartbeat.getSystemMetrics();
    }

    public boolean timeout() {
        long timeout = System.currentTimeMillis() - lastActiveTime;
        return timeout > WORKER_TIMEOUT_MS;
    }
}
